package com.becca.cake.controller.fixing;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.goods.GoodsFixingModel;
import com.becca.cake.service.fixing.IFixingService;
import com.becca.cake.util.JsonUtil;
/**
 * 配件管理
 * @copyright  尊星(上海)信息科技有限公司
 * @author yellow
 * @date 2016年6月13日 下午5:55:19
 */
@Controller
@RequestMapping("/fixingManager")
public class FixingAction extends BaseAction{
    private final static Log  logger = LogFactory.getLog(FixingAction.class);
    
    @Autowired
    private IFixingService iFixingService;
    
    /**
     * 查询配件列表
     * @param goodsModel
     * @return
     */
    @RequestMapping(value = "/queryFixings", method = RequestMethod.POST)
    @ResponseBody
    public String queryFixings(GoodsFixingModel goodsFixingModel,
                              @RequestParam(value = "existall", required = false) String existall,
                              @RequestParam(value = "statusArr", required = false) String statusArr){
        
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("goodsFixingModel", goodsFixingModel);
        try {
            // stmp1:查询配件数据
            List<GoodsFixingModel> list = this.iFixingService.queryFixings(map);
            // stmp2:获取配件总条数
            Long count = this.iFixingService.selectFixingCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 添加或修改配件信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/addOrUpdateFixing", method = RequestMethod.POST)
    @ResponseBody
    public  String addOrUpdateFixing(HttpServletRequest request, GoodsFixingModel goodsFixingModel) {
        //step1:获取图片file
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile files = multipartRequest.getFile("upload");
        try {
            this.iFixingService.insertOrUpdateFixing(goodsFixingModel, files, getTempFoderPath(),this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("保存配件信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    
    }
    
    /**
     * 启用/禁用配件
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/updateIsEnableById", method = RequestMethod.POST)
    @ResponseBody
    public String updateIsEnableById(@RequestParam(value = "fixingId", required = false) Integer fixingId,
                                   @RequestParam(value = "isEnable", required = false) Integer isEnable){
        try {
            this.iFixingService.updateIsEnableById(fixingId,isEnable,this.getUserSeesion().getTrueName());
            return JsonUtil.returnJsonSuccess("操作配件成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 可选/不可选配件
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/updIsOptionalById", method = RequestMethod.POST)
    @ResponseBody
    public String updIsOptionalById(@RequestParam(value = "fixingId", required = false) Integer fixingId,
                                   @RequestParam(value = "isOptional", required = false) Integer isOptional){
        try {
            this.iFixingService.updateIsOptionalById(fixingId,isOptional,this.getUserSeesion().getName());
            return JsonUtil.returnJsonSuccess("操作配件成功！");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 根据配件ID查询配件对象
     * @param goodsId
     * @return
     */
    @RequestMapping(value = "/queryFixingById", method = RequestMethod.POST)
    @ResponseBody
    public String queryFixingById(@RequestParam("fixingId") Integer fixingId){
        try {
            GoodsFixingModel goodsFixingModel = this.iFixingService.selectFixingById(fixingId);
            return JsonUtil.writeObjectFormJSON(goodsFixingModel);     
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
}
